package com.sghd.fight.module.game.service.skill.effect;

import com.sghd.common.utils.math.RandomUtils;
import com.sghd.fight.module.game.config.SkillConfig;
import com.sghd.fight.module.game.core.ChessBoard;
import com.sghd.fight.module.game.core.Grid;
import com.sghd.fight.module.game.core.model.Unit;
import com.sghd.fight.module.game.service.room.IRoom;
import com.sghd.fight.module.game.service.skill.Context;
import com.sghd.pojo.fight.game.SkillReport;
import com.sghd.pojo.fight.game.TargetReport;
import com.sghd.pojo.fight.game.UnitValue;
import com.sghd.pojo.fight.game.ValueItem;
import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author frank
 * @date 2018-12-13 14:20
 * @desc 地震术
 **/
@Component
public class EarthquakeEffect implements Effect {

    @Override
    public void execute(Context context, IRoom room, Unit owner, Unit target, Map<String, Object> ctx, SkillReport skillReport, TargetReport targetReport, SkillConfig config) {
        ChessBoard chessBoard = room.getChessBoard();
        //随机一列
        Grid[] grids = chessBoard.getGrids();
        int randomCol = RandomUtils.nextInt(chessBoard.getCol());
        skillReport.getIndexs().add(randomCol);//客户端需要

        int row = chessBoard.getRow();
        int start = randomCol * row;
        Set<Integer> eliminated = new HashSet<>();
        for (; start < (randomCol + 1) * row; start++) {
            eliminated.add(start);
            Grid grid = grids[start];
            owner.getMagic().collect(grid.getType());//收集
            grid.remove();//消除
        }
        int level = owner.getSkillLevel(config.getId());
        Map<String, Object> attack2Level = MapUtils.getMap(ctx, String.valueOf(level));
        int attack = MapUtils.getIntValue(attack2Level, ATTACK);
        attack = (int) (attack * getBet(owner, target));
        attack = fixHp(owner, target, attack, room, config);
        context.addValue(target, UnitValue.HP, -attack);//扣血
        targetReport.addValue(ValueItem.valueOf(UnitValue.HP, -attack));

        chessBoard.getEliminated().addAll(eliminated);
        chessBoard.fill();
        //自动消除
        autoElimi(room, owner, skillReport, chessBoard);
    }

    @Override
    public EffectType getType() {
        return EffectType.SK_EARTHQUAKE;
    }
}
